/*
 * Copyright 2011-2012 by The Regents of the University of California Licensed
 * under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. you may obtain a copy of the
 * License from
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.ucsd.db.bassarisk.query.path;

import edu.ucsd.db.bassarisk.query.path.DSDElements.DSDAggregate;
import edu.ucsd.db.bassarisk.query.path.DSDElements.DSDContext;
import edu.ucsd.db.bassarisk.query.path.DSDElements.DSDDescent;
import edu.ucsd.db.bassarisk.query.path.DSDElements.DSDFilter;
import edu.ucsd.db.bassarisk.schema.AttributeClass;
import edu.ucsd.db.bassarisk.treepattern.DescentAxis;

public class DSDFormatVisitor implements DSDElementVisitor<Void, StringBuilder> {
  private void printAxis(final DescentAxis axis, final StringBuilder sb) {
    switch (axis) {
    case Descendant:
      sb.append("/");
    case Child:
      sb.append("/");
    default:
      break;
    }
  }

  @Override
  public Void visitAggregate(final DSDAggregate elem, final StringBuilder args) {
    this.printAxis(elem.getAxis(), args);
    args.append("#");
    switch (elem.getFunction()) {
    case Count:
      args.append("count");
      break;
    case Empty:
      args.append("empty");
      break;
    case Mean:
      args.append("avg");
      break;
    case NonEmpty:
      args.append("exists");
      break;
    case Product:
      args.append("product");
      break;
    case Sum:
      args.append("sum");
      break;
    case Values:
      break;
    default:
      break;
    }
    args.append(":");
    if (elem.getSequenceClass() instanceof AttributeClass) {
      args.append("@");
    }
    args.append(elem.getSequenceClass().getName());
    return null;
  }

  @Override
  public Void visitContext(final DSDContext elem, final StringBuilder args) {
    args.append(".");
    return null;
  }

  @Override
  public Void visitDescent(final DSDDescent elem, final StringBuilder args) {
    this.printAxis(elem.getAxis(), args);
    if (elem.getSequenceClass() instanceof AttributeClass) {
      args.append("@");
    }
    args.append(elem.getSequenceClass().getName());
    return null;
  }

  @Override
  public Void visitFilter(final DSDFilter elem, final StringBuilder args) {
    args.append("[@");
    args.append(elem.getAttributeClass().getName()).append("=")
        .append(elem.getFilterValue());
    args.append("]");
    return null;
  }
}
